package hu.dynamicwebshop.gui;

import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.TextField;
import hu.dynamicwebshop.languagemanager.LanguageManager;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 *
 * @author Szecskó Zoltán <zoltan.szecsko@gmail.com>
 */
public class UserLayer extends HorizontalLayout
        implements ApplicationContextAware {

    private static final long serialVersionUID = 1L;
    private String width = "900";
    private String height = "40";
    private ApplicationContext ctx;
    private LanguageManager languageManager;
    private String countryCode;
    private Label usernameLabel;
    private TextField usernameField;
    private Label passwordLabel;
    private TextField passwordField;
    private Button loginButton;
    private Button registrationButton;

    public void createUserLayer() {
        setStyleName("userLayer");
        setHeight(height);
        setWidth(width);

        if (languageManager == null) {
            languageManager = (LanguageManager) ctx.getBean("languageManager");
            countryCode = languageManager.getCountryCode();
        }

        if (usernameLabel == null) {
            usernameLabel = new Label(languageManager.getOutputTextMap().get(countryCode + "usernameUserLayer"));
            addComponent(usernameLabel);
        } else if (usernameLabel.getParent() == null || !usernameLabel.getParent().getClass().equals(this)) {
            addComponent(usernameLabel);
        }

        if (usernameField == null) {
            usernameField = new TextField();
            addComponent(usernameField);
        } else if (usernameField.getParent() == null || !usernameField.getParent().getClass().equals(this)) {
            addComponent(usernameField);
        }

        if (passwordLabel == null) {
            passwordLabel = new Label(languageManager.getOutputTextMap().get(countryCode + "passwordRegistration"));
            addComponent(passwordLabel);
        } else if (passwordLabel.getParent() == null || !passwordLabel.getParent().getClass().equals(this)) {
            addComponent(passwordLabel);
        }

        if (passwordField == null) {
            passwordField = new TextField();
            addComponent(passwordField);
        } else if (passwordField.getParent() == null || !passwordField.getParent().getClass().equals(this)) {
            addComponent(passwordField);
        }

        if (loginButton == null) {
            loginButton = new Button(languageManager.getOutputTextMap().get(countryCode + "loginUserLayer"));

            loginButton.addClickListener(new Button.ClickListener() {

                @Override
                public void buttonClick(ClickEvent event) {
                    ((ContentLayer) ctx.getBean("contentLayer")).setUserContent(usernameField, passwordField);
                }
            });

            addComponent(loginButton);
        } else if (loginButton.getParent() == null || !loginButton.getParent().getClass().equals(this)) {
            addComponent(loginButton);
        }

        if (registrationButton == null) {
            registrationButton = new Button(languageManager.getOutputTextMap().get(countryCode + "registrationButtonRegistration"));
            registrationButton.addClickListener(new Button.ClickListener() {

                private static final long serialVersionUID = 1L;

                @Override
                public void buttonClick(ClickEvent event) {
                    ((ContentLayer) ctx.getBean("contentLayer")).setRegistrationContent();
                }
            });
            addComponent(registrationButton);
        } else if (registrationButton.getParent() == null || !registrationButton.getParent().getClass().equals(this)) {
            addComponent(registrationButton);
        }

        Button adminButton = new Button("Admin felület");
        adminButton.addClickListener(new Button.ClickListener() {
            private static final long serialVersionUID = 1L;

            @Override
            public void buttonClick(ClickEvent event) {
                ((ContentLayer) ctx.getBean("contentLayer")).setAdminContent();
            }
        });
        //addComponent(adminButton);
    }

    @Override
    public void setApplicationContext(ApplicationContext ctx)
            throws BeansException {
        this.ctx = ctx;
    }
}
